package jnpf.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_DuBanXiangMuEntity;
import jnpf.mapper.ZXGL_DuBanXiangMuMapper;
import jnpf.model.zxgl_dubanxiangmu.ZXGL_DuBanXiangMuPagination;
import jnpf.service.ZXGL_DuBanXiangMuService;
import jnpf.util.DateUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ZXGL_DuBanXiangMu
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-22
 */
@Service
public class ZXGL_DuBanXiangMuServiceImpl extends ServiceImpl<ZXGL_DuBanXiangMuMapper, ZXGL_DuBanXiangMuEntity> implements ZXGL_DuBanXiangMuService {


    @Autowired
    private UserProvider userProvider;
    @Resource
    PermissionsConfig permissionsConfig;

    @Override
    public List<ZXGL_DuBanXiangMuEntity> getList(ZXGL_DuBanXiangMuPagination zXGL_DuBanXiangMuPagination) {

        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_DuBanXiangMuEntity> zXGL_DuBanXiangMuQueryWrapper = new QueryWrapper<>();
        //TODO 增加权限逻辑
        UserInfo userInfo = userProvider.get();
        if (!userInfo.getIsAdministrator()) {
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds == null || (StringUtils.isNotBlank(alldataRoleId) && !roleIds.contains(alldataRoleId))) {
                String userId = userInfo.getUserId();
                //项目督办：创建人看本人数据；创建人部门负责人看本部门数据；
                // 创建人部门主管领导看所管理部门数据；主责人看自己负责项目数据；
                // 主责部门负责人看本部门负责项目数据；主责部门主管领导看所管理部门负责数据；
                zXGL_DuBanXiangMuQueryWrapper.lambda().and(wrapper -> wrapper
                        .eq(ZXGL_DuBanXiangMuEntity::getCreatorUserId, userId)//创建人看本人数据
                        .or()
                        .eq(ZXGL_DuBanXiangMuEntity::getDepartmentManager, userId)//创建人部门负责人看本部门数据
                        .or()
                        .eq(ZXGL_DuBanXiangMuEntity::getDepartmentHeadleader, userId) //创建人部门主管领导看所管理部门数据
                        .or()
                        .eq(ZXGL_DuBanXiangMuEntity::getZzr, userId)//主责人看自己负责项目数据
                        .or()
                        .eq(ZXGL_DuBanXiangMuEntity::getDepartmentManagerZzbm, userId)//主责部门负责人看本部门负责项目数据
                        .or()
                        .eq(ZXGL_DuBanXiangMuEntity::getDepartmentHeadleaderZzbm, userId)//主责部门主管领导看所管理部门负责数据
                );
            }
        }
        int zXGL_DuBanXiangMuNum = 0;
        if (StringUtil.isNotEmpty(zXGL_DuBanXiangMuPagination.getXmmc())) {
            zXGL_DuBanXiangMuNum++;
            zXGL_DuBanXiangMuQueryWrapper.lambda().like(ZXGL_DuBanXiangMuEntity::getXmmc, zXGL_DuBanXiangMuPagination.getXmmc());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanXiangMuPagination.getZzr())) {
            zXGL_DuBanXiangMuNum++;
            zXGL_DuBanXiangMuQueryWrapper.lambda().eq(ZXGL_DuBanXiangMuEntity::getZzr, zXGL_DuBanXiangMuPagination.getZzr());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanXiangMuPagination.getXmId())) {
            zXGL_DuBanXiangMuNum++;
            zXGL_DuBanXiangMuQueryWrapper.lambda().eq(ZXGL_DuBanXiangMuEntity::getXmid, zXGL_DuBanXiangMuPagination.getXmId());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanXiangMuPagination.getDbqx())) {
            zXGL_DuBanXiangMuNum++;
            List<String> DbqxList = zXGL_DuBanXiangMuPagination.getDbqx();
            Long fir = Long.valueOf(DbqxList.get(0));
            Long sec = Long.valueOf(DbqxList.get(1));

            zXGL_DuBanXiangMuQueryWrapper.lambda().ge(ZXGL_DuBanXiangMuEntity::getDbqx, new Date(fir))
                    .le(ZXGL_DuBanXiangMuEntity::getDbqx, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (AllIdList.size() > 0) {
            zXGL_DuBanXiangMuQueryWrapper.lambda().in(ZXGL_DuBanXiangMuEntity::getId, AllIdList);
        }
        //排序
//        if (StringUtil.isEmpty(zXGL_DuBanXiangMuPagination.getSidx())) {
//            zXGL_DuBanXiangMuQueryWrapper.lambda().orderByDesc(ZXGL_DuBanXiangMuEntity::getId);
//        } else {
//            try {
//                String sidx = zXGL_DuBanXiangMuPagination.getSidx();
//                ZXGL_DuBanXiangMuEntity zXGL_DuBanXiangMuEntity = new ZXGL_DuBanXiangMuEntity();
//                Field declaredField = zXGL_DuBanXiangMuEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_DuBanXiangMuQueryWrapper = "asc".equals(zXGL_DuBanXiangMuPagination.getSort().toLowerCase()) ? zXGL_DuBanXiangMuQueryWrapper.orderByAsc(value) : zXGL_DuBanXiangMuQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
        zXGL_DuBanXiangMuQueryWrapper.lambda().orderByDesc(ZXGL_DuBanXiangMuEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_DuBanXiangMuEntity> page = new Page<>(zXGL_DuBanXiangMuPagination.getCurrentPage(), zXGL_DuBanXiangMuPagination.getPageSize());
            IPage<ZXGL_DuBanXiangMuEntity> userIPage = this.page(page, zXGL_DuBanXiangMuQueryWrapper);
            return zXGL_DuBanXiangMuPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_DuBanXiangMuEntity> list = new ArrayList();
            return zXGL_DuBanXiangMuPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_DuBanXiangMuEntity> getTypeList(ZXGL_DuBanXiangMuPagination zXGL_DuBanXiangMuPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_DuBanXiangMuEntity> zXGL_DuBanXiangMuQueryWrapper = new QueryWrapper<>();
        int zXGL_DuBanXiangMuNum = 0;
        if (StringUtil.isNotEmpty(zXGL_DuBanXiangMuPagination.getXmmc())) {
            zXGL_DuBanXiangMuNum++;
            zXGL_DuBanXiangMuQueryWrapper.lambda().like(ZXGL_DuBanXiangMuEntity::getXmmc, zXGL_DuBanXiangMuPagination.getXmmc());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanXiangMuPagination.getZzr())) {
            zXGL_DuBanXiangMuNum++;
            zXGL_DuBanXiangMuQueryWrapper.lambda().eq(ZXGL_DuBanXiangMuEntity::getZzr, zXGL_DuBanXiangMuPagination.getZzr());
        }

        if (StringUtil.isNotEmpty(zXGL_DuBanXiangMuPagination.getDbqx())) {
            zXGL_DuBanXiangMuNum++;
            List<String> DbqxList = zXGL_DuBanXiangMuPagination.getDbqx();
            Long fir = Long.valueOf(DbqxList.get(0));
            Long sec = Long.valueOf(DbqxList.get(1));

            zXGL_DuBanXiangMuQueryWrapper.lambda().ge(ZXGL_DuBanXiangMuEntity::getDbqx, new Date(fir))
                    .le(ZXGL_DuBanXiangMuEntity::getDbqx, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (AllIdList.size() > 0) {
            zXGL_DuBanXiangMuQueryWrapper.lambda().in(ZXGL_DuBanXiangMuEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_DuBanXiangMuPagination.getSidx())) {
            zXGL_DuBanXiangMuQueryWrapper.lambda().orderByDesc(ZXGL_DuBanXiangMuEntity::getId);
        } else {
            try {
                String sidx = zXGL_DuBanXiangMuPagination.getSidx();
                ZXGL_DuBanXiangMuEntity zXGL_DuBanXiangMuEntity = new ZXGL_DuBanXiangMuEntity();
                Field declaredField = zXGL_DuBanXiangMuEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_DuBanXiangMuQueryWrapper = "asc".equals(zXGL_DuBanXiangMuPagination.getSort().toLowerCase()) ? zXGL_DuBanXiangMuQueryWrapper.orderByAsc(value) : zXGL_DuBanXiangMuQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_DuBanXiangMuEntity> page = new Page<>(zXGL_DuBanXiangMuPagination.getCurrentPage(), zXGL_DuBanXiangMuPagination.getPageSize());
                IPage<ZXGL_DuBanXiangMuEntity> userIPage = this.page(page, zXGL_DuBanXiangMuQueryWrapper);
                return zXGL_DuBanXiangMuPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_DuBanXiangMuEntity> list = new ArrayList();
                return zXGL_DuBanXiangMuPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_DuBanXiangMuQueryWrapper);
        }
    }

    @Override
    public ZXGL_DuBanXiangMuEntity getInfo(String id) {
        QueryWrapper<ZXGL_DuBanXiangMuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_DuBanXiangMuEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_DuBanXiangMuEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_DuBanXiangMuEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public List<ZXGL_DuBanXiangMuEntity> getList(String xmId) {
        QueryWrapper<ZXGL_DuBanXiangMuEntity> zXGL_DuBanXiangMuQueryWrapper = new QueryWrapper<>();
        zXGL_DuBanXiangMuQueryWrapper.lambda().eq(ZXGL_DuBanXiangMuEntity::getXmid, xmId);
        return this.list(zXGL_DuBanXiangMuQueryWrapper);
    }

    @Override
    public void delete(ZXGL_DuBanXiangMuEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}